package com.scrx.ymg.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.Gson;
import com.scrx.ymg.entity.OrderForm;
import com.scrx.ymg.entity.OrderFormDetails;
import com.scrx.ymg.entity.OrderInform;
import com.scrx.ymg.entity.OrderToInform;
import com.scrx.ymg.entity.OrderToInformData;

public class WeixinUtil {
	
	/**
     * 发送模板消息
     * appId 公众账号的唯一标识
     * appSecret 公众账号的密钥
     * openId 用户标识
     */
    public static void send_template_message(String openId,OrderForm order,List<OrderFormDetails> details) {
    	//System.out.println(PayConfigUtil.access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+PayConfigUtil.access_token;
        StringBuffer strb=new StringBuffer();
        for (int i = 0; i < details.size(); i++) {
        	strb.append(details.get(i).getProductName()+",");
		}
        strb.substring(0, strb.length()-1);
        OrderToInform temp =new OrderToInform();
        OrderToInformData data=new OrderToInformData();
        data.setKeyword1(new OrderInform(order.getOrderFormId()));
        data.setKeyword2(new OrderInform(strb.toString()));
        data.setKeyword3(new OrderInform(order.getUserName()));
        data.setKeyword4(new OrderInform(order.getAddress()));
        data.setKeyword5(new OrderInform("待发货"));
        data.setKeyword6(new OrderInform(order.getTotalMoney().toString()));
        data.setKeyword7(new OrderInform(order.getOrderFormTime()));
        data.setKeyword8(new OrderInform("文山三七平台客服电话：17783036963"));
        temp.setData(data);
        temp.setForm_id(order.getPrepayId());
        //temp.setPage(page);
        temp.setTouser(openId);
        temp.setTemplate_id(PayConfigUtil.Template_ID);
       // temp.setTopcolor("#173177");
        Gson gson = new Gson();
        String jsonString = gson.toJson(temp);                                           
        StringBuffer jsonObject =httpsRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
    }

	 /**
	     * 
	     * @param requestUrl     接口地址
	     * @param requestMethod  请求方法：POST、GET...
	     * @param output         接口入参
	     * @param needCert       是否需要数字证书
	     * @return
	     */
	  public  static StringBuffer httpsRequest(String requestUrl, String requestMethod,
	    		String output){
	    	try {
		        URL url = new URL(requestUrl);
		        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
		        connection.setDoOutput(true);
		        connection.setDoInput(true);
		        connection.setUseCaches(false);
		        connection.setRequestMethod(requestMethod);
		        if (null != output) {
		            OutputStream outputStream = connection.getOutputStream();
		            outputStream.write(output.getBytes("UTF-8"));
		            outputStream.close();
		        }

		        // 从输入流读取返回内容
		        InputStream inputStream = connection.getInputStream();
		        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
		        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		        String str = null;
		        StringBuffer buffer = new StringBuffer();
		        while ((str = bufferedReader.readLine()) != null) {
		            buffer.append(str);
		        }
		        bufferedReader.close();
		        inputStreamReader.close();
		        inputStream.close();
		        inputStream = null;
		        connection.disconnect();
		        return buffer;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	    	
	    }

}
